package com.oocl.chatroom.client.listener;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;










import org.json.JSONException;
import org.json.JSONObject;

import com.oocl.chatroom.client.Client;
import com.oocl.chatroom.client.ui.ChatUI;
import com.oocl.chatroom.network.protocol.Message;

public class ReceiverListener implements Runnable{
	
	private Client client;
	
	private ChatUI chatUI = null;
	
	private List<String> nameList;
	
	
	public ReceiverListener(Client client) {
		super();
		this.client = client;
		this.nameList = new ArrayList<String>();
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public void run() {
		while (true) {

			Message msg = null;
			try {
				if(client==null){
					client = null;
					Message serverDownMsg = new Message("server is shutting down",3,"system","",System.currentTimeMillis());
					chatUI.printServerShutdown(serverDownMsg);
					break;
				}
				BufferedReader in = client.getIn();
				String json =  in.readLine();
				msg = changeToMessage(json);
				switch (msg.getType()) {
				  case 0:
					  chatUI.msgPrint(msg);
					  break;
				  case 1:
					  chatUI.msgPrint(msg);
					  break;
				  case 2:
					  String[] ns = getUpdatedNames(msg);
					  chatUI.listRefresh(ns);
					  chatUI.msgPrint(msg);
					  break;
				  case 3:
					  String[] nams = getUpdatedNames(msg);
					  chatUI.listRefresh(nams);
					  chatUI.msgPrint(msg);
					  break;
				 case 4:
					  chatUI = new ChatUI(client);
					  chatUI.setVisible(true);
					  String[] names = initNames(msg);
					  chatUI.listRefresh(names);
					  break;
				 case 9:
					    client = null;
						Message serverDownMsg = new Message("server is shutting down",3,"system","",System.currentTimeMillis());
						chatUI.printServerShutdown(serverDownMsg);
						break;
					  default:break;
				}
			} catch (IOException e) {
				client = null;
				Message serverDownMsg = new Message("server is shutting down",3,"system","",System.currentTimeMillis());
				chatUI.printServerShutdown(serverDownMsg);
				break;
			}
			
		}

	}

	public boolean entryMsg(){
		boolean flag=false;
		Message msg=null;
		try {
			BufferedReader in = client.getIn();
			String json =  in.readLine();
			msg = changeToMessage(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		switch(msg.getType()){
		case 7:
			flag=false;
			break;
		case 8:
			flag=true;
			break;
		}
		return flag;
	}
	
	private Message changeToMessage(String json) {
		Message msg = new Message();
		 System.out.println(json);
		try {
			JSONObject jOb = new JSONObject(json);
			System.out.println(jOb.getInt("type"));
			msg.setContent( jOb.getString("content"));
			msg.setDate(jOb.getLong("date"));
			msg.setSender( jOb.getString("sender"));
			msg.setReceiver(jOb.getString("receiver"));
			msg.setType(Integer.parseInt(jOb.getString("type")));
		}catch (JSONException e) {
			e.printStackTrace();
		}

		return msg;
	}

	private String[] getUpdatedNames(Message msg){
		String[] names = null ;
		if(msg.getType()==2){
			nameList.add(msg.getContent());
			names = new String[nameList.size()+1];
			for(int i=0 ;i<nameList.size();i++)
				names[i] = nameList.get(i);
		}else if(msg.getType()==3){
			names = new String[nameList.size()-1];
			for(int i=0 ;i<nameList.size();i++){
				if(nameList.get(i).equals(msg.getContent())){
					System.out.println(nameList.remove(i));
					break;
				}
			}
			for(int i=0;i<names.length;i++)
				names[i] = nameList.get(i);
		}
		return names;
	}

	private String[] initNames(Message msg) {
		// TODO Auto-generated method stub
		String content = msg.getContent();
		String[] strs = content.split(";");
		for(int i=0;i<strs.length;i++){
			
			nameList.add(strs[i]);
		}
		return strs;
	}

}
